package com.bdqn.springbootwebsocket.comm;

import jakarta.servlet.http.HttpSession;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;

public class GetHttpSessionConfigure extends ServerEndpointConfig.Configurator {

    //获取HttpSession

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        //将HttpSession存入ServerEndpointConfig中
        //这样在WebSocket中可以通过ServerEndpointConfig获取HttpSession它实现了EndpointConfigurator接口
        //从而可以获取HttpSession
        //从而实现跨域访问
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
    }
}
